var arr = [], //随机歌曲的下标
	j = 0, //歌曲数
	achievement = 0, //分数	
	ad = $(".option audio"), //播放器
	lis = $(".option .answer"), //答案选项
	pages=$(".page"),
	second = $(".option_60S"),
	result = $("#result"),
	reAnswer = $(".answer-result"),
	duanwei = $(".duanwei img")
	reText = $(".text");

//随机30首不同歌曲
function num() {
	for(var i = 0; i > -1; i++) {
		var num = Math.floor(Math.random() * 51);	
		if(arr.indexOf(num) == -1) {
			arr.push(num);
		}
		if(arr.length == 30) {
			break;
		}
	}
}
num();

var isOn = true;

//判断答案正确与否，答对加一分
function myFunction(jso) {
	setLi(jso);
	lis.on("click", function(e) {		
		if(isOn == false) {
			isOn = true;					
			if(($(this).attr("data-answer")) == (jso[arr[j]].answer)) {
				achievement++;				
			}
			j++;
			setLi(jso);
		}
	});
}

//把答案以及歌曲填充进选项标签
function setLi(jso) {
	ad.attr("src", jso[arr[j]].audio);
	lis.css("color","red");
	$.each(lis, function(i) {
		$(this).text(jso[arr[j]].item[i]);
	})
	if(isOn == true) {
		var time = setTimeout(function() {
			isOn = false;
			lis.css("color","#333");
			clearTimeout(time);
		}, 2000)
	}
}

var opt = $(".option"); //选项容器
var classli = $(".classics");
var loadBtn = $("#load .btn"); //加载页跳过按钮
var indexBtn = $("#index .btn"); //主页开始按钮
var index = $("#index"); //主页
loadBtn.on("click", function() {
	$(this).parent().fadeOut();
	index.fadeIn();
})
indexBtn.on("click", function() {
	$(this).parent().fadeOut();
	classli.fadeIn();
	opt.fadeIn();
	dat("../data/music.json", myFunction);
	donghua();
	daoshu();
})
//ajax请求
function dat(url, callBack) {
	$.ajax({
		type: "get",
		url: url,
		async: true,
		beforeSend: function(jso) //发送前运行
		{
			//		console.log(jso);
		},
		complete: function(jso) //不管请求成功与否都会运行
		{
			//		console.log(jso);
		},
		success: function(jso) //成功得到返回值时运行
		{			
			callBack(jso)
		},
		error: function(err, type, missage) //错误对象，类型，信息
		{
			console.log(err);
			console.log(type);
			console.log(missage);
		},
	});
}
var time2;
function donghua()
{
	var n =1;
	time2 = setInterval(function(){
		pages.css("display","none")
		pages.eq(n).css("display","block");
		n++;
		n>pages.length-1?n=0:null;
	},10000)
}

function daoshu()
{
	var s = 60;
	var  time3= setInterval(function(){
		s--;
		second.text(s+"s");
		if(s==0)
		{
			clearInterval(time3);
			clearInterval(time2)
			rest();
		}
	},1000)
}
function rest()
{
	opt.css("display","none");
	pages.css("display","none");
	result.css("display","block");
	reAnswer.text("猜中"+achievement+"首");
	switch(true)
	{
		case achievement<=5:
			duanwei.attr("src","../img/green.png");
			reText.text(arrText[0]);
			break;
		case achievement<=10 && achievement>5:
			duanwei.attr("src","../img/whiteyin.png");
			reText.text(arrText[1]);
			break;
		case achievement<=15 && achievement>10:
			duanwei.attr("src","../img/gold.png");
			reText.text(arrText[2]);
			break;
		case achievement<=20 && achievement>15:
			duanwei.attr("src","../img/bojin.png");
			reText.text(arrText[3]);
			break;
		case achievement<=30 && achievement>20:
			duanwei.attr("src","../img/bojin.png");
			reText.text(arrText[4]);
			break;
	}
	ad.remove();
}
var arrText = [
	"青铜：渣渣青铜怕过谁，音乐一响就胡怼！这位盆友，没事多听听酷狗音乐吧！传说中的音乐黑洞就素你啦！",
	"白银：萌萌哒白银新手！我们知道你已经尽力了！晒晒结果吧，说不定可以萌到个对象呢~",
	"黄金：失敬失敬！十二星座黄金“音”斗士，你是哪一宫？向你的基友们寄送挑战书吧！",
	"铂金：说，你是不是隐匿多年的酷狗会员？对音乐的涉猎有点广泛啊！快晒出段位，接受世人的瞻仰吧！",
	"钻石：是天性压抑了太久，还是一个人寂寞到癫狂？我们的题库都不够你做了啊！行走的中华小曲库，快晒结果独孤求败吧！"
]
